package com.singzi.leetcode.N100;

public class T0053 {
    public static void main(String[] args) {
        int []a = {-2,1};
        Solution0053 solution0053 = new Solution0053();
        System.out.println(solution0053.maxSubArray(a));
    }
}
class Solution0053 {
    public int maxSubArray(int[] nums) {
        if(nums.length==1) return nums[0];
        int sum = 0, maxSum = 0,max = -9999;
        for(int i = 0;i<nums.length;i++){
            if(nums[i]>max) max = nums[i];
            sum += nums[i];
            if(sum >maxSum){
                maxSum = sum;
            }
            else if(sum < 0){
                sum = 0;
            }
        }
        if(max<0)
            maxSum = max;
        return maxSum;

    }
}